<?php
session_start();
if (!isset($_SESSION['admin'])) { header('Location: index.php'); exit; }
$pid = $_GET['pid'] ?? '';
$projects = file_exists('../data/projects.json') ? json_decode(file_get_contents('../data/projects.json'), true) : [];
if (!$pid || !isset($projects[$pid])) { echo '项目不存在'; exit; }
$dir = "../data/project_$pid/";
$dataFile = $dir.'screenshots.json';
$imgDir = $dir.'screenshots/';
if (!is_dir($imgDir)) mkdir($imgDir, 0777, true);
$screens = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
// 上传
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['img'])) {
    $file = $_FILES['img'];
    if ($file['error'] === 0) {
        $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['png','jpg','jpeg','webp'])) {
            $fname = uniqid('img_').'.'.$ext;
            move_uploaded_file($file['tmp_name'], $imgDir.$fname);
            $screens[] = $fname;
            file_put_contents($dataFile, json_encode($screens, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
            header('Location: screenshots.php?pid='.$pid); exit;
        }
    }
}
// 删除
if (isset($_GET['del'])) {
    $del = $_GET['del'];
    if (($k = array_search($del, $screens)) !== false) {
        unlink($imgDir.$del);
        array_splice($screens, $k, 1);
        file_put_contents($dataFile, json_encode($screens, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
    }
    header('Location: screenshots.php?pid='.$pid); exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>截图管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
    .upload-area {
        border: 2px dashed #007aff;
        border-radius: 14px;
        background: #f8f9fa;
        padding: 48px 0 36px 0;
        text-align: center;
        margin-bottom: 32px;
        cursor: pointer;
        transition: border-color 0.2s;
        font-size: 1.2rem;
    }
    .upload-area.dragover { border-color: #005bb5; background: #eaf4ff; }
    .upload-area input[type=file] { display: none; }
    .upload-tip { color: #888; font-size: 1rem; margin-top: 8px; }
    .back-btn-top { margin-bottom: 24px; font-size: 1.1rem; padding: 10px 28px; }
    </style>
</head>
<body>
<div class="container py-4">
    <h4 class="mb-3">截图管理 - <?=htmlspecialchars($projects[$pid]['name'])?></h4>
    <form method="post" enctype="multipart/form-data" id="upload-form">
        <label class="upload-area mb-4" id="upload-area">
            <span>点击或拖拽图片到此上传截图</span>
            <input type="file" name="img" id="img-upload" accept="image/*">
            <div class="upload-tip">支持PNG/JPG/JPEG/WEBP，建议尺寸竖屏手机截图</div>
        </label>
    </form>
    <div class="row g-3">
        <?php foreach($screens as $img): ?>
        <div class="col-6 col-md-4 col-lg-3">
            <div class="card">
                <img src="<?=$imgDir.$img?>" class="card-img-top" style="object-fit:cover;height:180px;">
                <div class="card-body p-2 text-center">
                    <a href="?pid=<?=$pid?>&del=<?=$img?>" class="btn btn-danger btn-sm" onclick="return confirm('确定删除？')">删除</a>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <div class="d-flex justify-content-end gap-3 mt-4">
        <a href="project_dashboard.php?pid=<?=$pid?>" class="btn btn-outline-secondary btn-lg px-5">返回</a>
    </div>
</div>
<script>
const uploadArea = document.getElementById('upload-area');
const fileInput = document.getElementById('img-upload');
// 点击虚线框触发文件选择
uploadArea.onclick = function(e) {
    if(e.target===uploadArea || e.target.classList.contains('upload-tip')) fileInput.click();
};
// 拖拽上传
uploadArea.addEventListener('dragover', function(e) {
    e.preventDefault(); uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', function(e) {
    e.preventDefault(); uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', function(e) {
    e.preventDefault(); uploadArea.classList.remove('dragover');
    if (e.dataTransfer.files.length) {
        fileInput.files = e.dataTransfer.files;
        document.getElementById('upload-form').submit();
    }
});
fileInput.onchange = function() {
    if (fileInput.files.length) document.getElementById('upload-form').submit();
};
</script>
</body>
</html> 